﻿using NineSeas.Services;
using NineSeas.ViewModels;
using Orchard;
using Orchard.Core.Contents;
using Orchard.ContentManagement;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc;
using Orchard.Security;
using Orchard.Themes;
using Orchard.Users.Models;
using Orchard.Users.Services;
using System.Web.Mvc;
using System.Linq;
using NineSeas.Models;
using Orchard.Core.Common.Models;
namespace NineSeas.Controllers
{
    public class AuthorsController : Controller
    {
        private const int _defaultPagesize = 8;
        private readonly IOrchardServices _services;
        private readonly IAuthorService _authorService;
        private readonly IUserService _userService;
        private readonly IBookService _bookService;
        private readonly ISocialStatisticService _socialStatisticService;
        private dynamic Shape { get; set; }
        private Localizer T { get; set; }

        public AuthorsController(IShapeFactory shapeFactory, IOrchardServices services, 
            IAuthorService authorSerice, IUserService userService,
            ISocialStatisticService socialStatisticService,
            IBookService bookService)
        {
            this._services = services;
            this._authorService = authorSerice;
            this._userService = userService;
            this._bookService = bookService;
            this._socialStatisticService = socialStatisticService;
            this.Shape = shapeFactory;
            T = NullLocalizer.Instance;
        }

        [Themed]
        public ActionResult Index(AuthorSearchViewModel search, int currentPage = 0, int pageSize = 24)
        {
            return Index(search, new PaginationInfo(currentPage, pageSize));
        }

        private ActionResult Index(AuthorSearchViewModel search, PaginationInfo paging)
        {
            var authors = _authorService.Search(search, paging);

            _services.WorkContext.Layout.Zones["Featured"].Add(_services.New.Authors_Index_Commands(Search: search), "0");
            return new ShapeResult(this, _services.New.Authors_Index(
                    Authors: authors,
                    Paging: paging,
                    Search: search
                ));
        }
        
        [Themed]
        [HttpGet]
        public ActionResult Details(int id, int pageIndex = 0, int pageSize = _defaultPagesize)
        {
            return Details(id, new PaginationInfo(pageIndex, pageSize));
        }

        private ActionResult Details(int id, PaginationInfo paging)
        {
            var user = _services.ContentManager.Get<UserPart>(id);

            var books = _bookService.GetBooksByAuthor(id, paging);
            var author = user != null ? user.As<AuthorProfilePart>() : null;
            var pager = Shape.Pager(paging).TotalItemCount(paging.TotalRecords);
            var partVersion = user != null ? user.As<ContentPart<CommonPartVersionRecord>>() : null;
            
            return new ShapeResult(this, _services.New.Authors_Author_Details(
                    User: user,
                    Author: author,
                    Books: books,
                    Paging: paging,
                    Pager: pager,
                    JoinedDate: partVersion != null ? partVersion.Record.CreatedUtc.Value.ToString("dd/MM/yyyy hh:mm:ss") : "",
                    IsLiked: _socialStatisticService.IsLiked(author)
                ));
        }

        public JsonResult Like(int id, string like)
        {
            try
            {
                var author = _services.ContentManager.Get<AuthorProfilePart>(id);
                _socialStatisticService.UpdateLikes(author, "1".Equals(like));
                return this.Json(new { success = true, caption = "1".Equals(like) ? T("Liked").Text : T("Like").Text }, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return this.Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}